﻿
Imports std

Partial Public Class node
    Public ReadOnly id As Int64 = Nothing
    Public ReadOnly p As locked_properties = Nothing
    Private initialized As Boolean = False

    Friend Function waitfor_initialized() As Boolean
        Return waitfor(Function() initialized)
    End Function

    Public Sub destroy()
        p.destroy()
    End Sub

    Private Sub start()
        Dim sn As pointer(Of [set](Of Int64)) = Nothing
        Dim ec As eventComb = Nothing
        Dim it As [set](Of Int64).iterator = Nothing
        assert_begin(New eventComb(Function() As Boolean
                                       sn = New pointer(Of [set](Of Int64))()
                                       ec = sub_nodes(sn)
                                       Return waitfor(ec) AndAlso
                                              goto_next()
                                   End Function,
                                   Function() As Boolean
                                       If ec.end_result() AndAlso Not +sn Is Nothing Then
                                           it = (+sn).begin()
                                           Return goto_next()
                                       Else
                                           Return goto_last()
                                       End If
                                   End Function,
                                   Function() As Boolean
                                       If it = (+sn).end() Then
                                           Return goto_next()
                                       Else
                                           Dim id As Int64 = 0
                                           id = (+it)
                                           it += 1
                                           Dim n As node = Nothing
                                           n = New node(id)
                                           If fs.add_node(n) Then
                                               Return n.waitfor_initialized()
                                           Else
                                               raiseError(errorHandle.errorType.warning,
                                                          "failed to insert node with id ", (id),
                                                          ", a node with the same id may be existing already.")
                                               Return True
                                           End If
                                       End If
                                   End Function,
                                   Function() As Boolean
                                       initialized = True
                                       Return goto_end()
                                   End Function))
    End Sub
End Class
